package com.taiyi.renaissancemall.ui.main.personshare


import android.content.ClipboardManager
import android.content.Context
import android.graphics.Bitmap
import android.os.Bundle
import android.view.View
import com.taiyi.renaissancemall.R
import com.taiyi.renaissancemall.Utils.ImageToFileUtils
import com.taiyi.renaissancemall.Utils.ToastUtils
import com.taiyi.renaissancemall.Utils.loadCircle
import com.taiyi.renaissancemall.base.BaseActivity
import com.taiyi.renaissancemall.base.MyApp
import com.taiyi.renaissancemall.bean.PersonShareBean
import com.taiyi.renaissancemall.wxapi.WXEntryActivity
import com.taiyi.renaissancemall.zxing.ZXingUtils
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX
import com.tencent.mm.opensdk.modelmsg.WXImageObject
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage
import kotlinx.android.synthetic.main.activity_person_share.*
import kotlinx.android.synthetic.main.base_top_white_background_layout.*
import java.io.ByteArrayOutputStream

class PersonShareActivity : BaseActivity<PersonShareView, PersonSharePresent>(),
    PersonShareView {
    var inviteCode = ""
    override fun getLayoutId(): Int = R.layout.activity_person_share

    override fun initPresenter(): PersonSharePresent =
        PersonSharePresent()

    override fun initViews(savedInstanceState: Bundle?) {
        titleTv.text = getString(R.string.personShare)
        backIv.setOnClickListener { finish() }
        copyLinkIv.setOnClickListener {
            val cm =
                getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            // 将文本内容放到系统剪贴板里。
            // 将文本内容放到系统剪贴板里。
            cm.text = inviteCode
            ToastUtils.showLongToast("复制成功")
        }
        sendImageIv.setOnClickListener {
            top.visibility = View.GONE
            try {
                WXEntryActivity.type = 2
                MyApp.getWxApi().registerApp(MyApp.APP_ID)
                var shareBitmap = ImageToFileUtils.captureView(rl_share_bg)
                //Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.send_img);
                //初始化 WXImageObject 和 WXMediaMessage 对象
                val imgObj = WXImageObject(shareBitmap)
                val msg = WXMediaMessage()
                msg.mediaObject = imgObj
                //设置缩略图
                val thumbBmp =
                    Bitmap.createScaledBitmap(shareBitmap, 100, 100, true)
                val baos = ByteArrayOutputStream()
                thumbBmp.compress(Bitmap.CompressFormat.JPEG, 100, baos)
                //                    shareBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                val data = baos.toByteArray()
                thumbBmp.recycle()
                //                    shareBitmap.recycle();
                msg.thumbData = data
                //构造一个Req
                val req = SendMessageToWX.Req()
                req.transaction = System.currentTimeMillis().toString()
                req.message = msg
                req.scene = SendMessageToWX.Req.WXSceneSession
                // req.userOpenId = getOpenId();
                //调用api接口，发送数据到微信
                MyApp.getWxApi().sendReq(req)
            } catch (throwable: Throwable) {
                throwable.printStackTrace()
            }
        }
    }

    override fun initData() {
        showProgress()
        presenter.getShareInfo()
    }

    override fun initImmersionBar() {
        super.initImmersionBar()
        mImmersionBar.titleBar(top).init()
    }

    override fun initEvent() {

    }

    override fun getInfoSuccess(personShareBean: PersonShareBean) {
        hideProgress()
        var headimg = personShareBean.headimg
        if (headimg != null) {
            avatarIv.loadCircle(headimg)
        }
        inviteCode = personShareBean.url
        inviteCodeTv.text = getString(R.string.inviteCode) + "：${personShareBean.invitecode}"
        var createQRImage = ZXingUtils.createQRImage(personShareBean.url, 127, 127)
        qrCodeIv.setImageBitmap(createQRImage)
    }

    override fun getInfoFailure(msg: String?) {
        hideProgress()
        toast(msg)
    }
}
